package com.antovita.militarymonkey.screens;

import com.antovita.militarymonkey.MyGdxGame;
import com.antovita.militarymonkey.service.Textures;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class LoadScreen extends Screen {
	
	private SpriteBatch batch;
	private Sprite sprite1;
	private Sprite sprite2;
	private Sprite sprite3;
	private long delta;
	float w;
	float h; 
	
	public LoadScreen(MyGdxGame gdxGame) {
		super(gdxGame);
	}

	@Override
	public void create() {
		
		w = Gdx.graphics.getWidth();
		h = Gdx.graphics.getHeight();
		
		batch = new SpriteBatch();
		Texture.setEnforcePotImages(false);
		Textures.loadSprites("data/bangLoad.png",  1 , 1);
		Textures.loadSprites("data/lgdx.png",  1 , 1);
		Textures.loadSprites("data/monkeyLoad.png",  1 , 1);
		
		sprite1 = new Sprite(Textures.getTextureRegions("data/monkeyLoad.png")[0]);
		if (sprite1.getWidth() > w) {
			sprite1.setSize(w, sprite1.getHeight() * (w / sprite1.getWidth()));
		}
		sprite1.setPosition((w - sprite1.getWidth()) / 2, (h - sprite1.getHeight()) / 2);
		
		sprite2 = new Sprite(Textures.getTextureRegions("data/bangLoad.png")[0]);
		if (w > sprite2.getWidth()) {
			sprite2.setSize(w, sprite2.getHeight() * (w / sprite2.getWidth()));
		}
		if (h > sprite2.getHeight()) {
			sprite2.setSize(sprite2.getWidth() * (h / sprite2.getHeight()), h);
		}
		sprite2.setPosition((w - sprite2.getWidth()) / 2, (h - sprite2.getHeight()) / 2);
		
		sprite3 = new Sprite(Textures.getTextureRegions("data/lgdx.png")[0]);
		if (sprite3.getWidth() > w) {
			sprite3.setSize(w, sprite3.getHeight() * (w / sprite3.getWidth()));
		}
		sprite3.setPosition((w - sprite3.getWidth()) / 2, (h - sprite3.getHeight()) / 2);
		delta = 0;
	}

	@Override
	public void render() {
		if (delta == 0)
			delta = System.currentTimeMillis();
		if (System.currentTimeMillis() - delta >= 1000) {
			Gdx.gl.glClearColor(1, 1, 1, 1); // 187 221 223
			Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
			batch.begin();
			sprite3.draw(batch);
			batch.end();
		}
		else {
			Gdx.gl.glClearColor(1, 1, 1, 1); // 187 221 223
			Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
			batch.begin();
			sprite2.draw(batch);
			sprite1.draw(batch);
			batch.end();
		}		
	}
}
